package com.systop.party08.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.systop.party08.R;
import com.systop.party08.adapter.FragPagerAdapter;
import com.systop.party08.base.BaseFragment;
import com.systop.party08.entity.HomeEntity;
import com.systop.party08.entity.MyHomeEntity;
import com.systop.party08.entity.TabVpEntity;
import com.systop.party08.http.MyStringCallback;
import com.systop.party08.utils.GeneralTools;
import com.systop.party08.utils.SPUtils;
import com.systop.party08.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.systop.party08.http.ComUrl.INDEX;
import static com.systop.party08.utils.StaticDateUtils.SP_TOKEN;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyHomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyHomeFragment extends BaseFragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    @BindView(R.id.home_tab)
    TabLayout homeTab;

    @BindView(R.id.home_vp)
    ViewPager homeVp;

    @BindView(R.id.home_refresh)
    SmartRefreshLayout refresh;

    private View view;

    public MyHomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MyHomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MyHomeFragment newInstance(String param1, String param2) {
        MyHomeFragment fragment = new MyHomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        refresh.setEnableLoadMore(false);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                okhttpIndex();
            }
        });
        refresh.autoRefresh();
        return view;
    }

    private void okhttpIndex() {
        //{"msg":"请求成功","status":"200","data":{"lmList":[{"name":"政策文件","id":6},{"name":"活动证管理","id":12},{"name":"服务指南","id":16},{"name":"风采展示","id":21},{"name":"党员学习之友","id":27},{"name":"网上公示","id":22},{"name":"党建动态","id":17},{"name":"新闻动态","id":36},{"name":"最新","id":40}]}}
        String tabStr = "{\"msg\":\"请求成功\",\"status\":\"200\",\"data\":{\"lmList\":[{\"name\":\"政策文件\",\"id\":6},{\"name\":\"活动证管理\",\"id\":12},{\"name\":\"服务指南\",\"id\":16},{\"name\":\"风采展示\",\"id\":21},{\"name\":\"党员学习之友\",\"id\":27},{\"name\":\"网上公示\",\"id\":22},{\"name\":\"党建动态\",\"id\":17},{\"name\":\"新闻动态\",\"id\":36},{\"name\":\"最新\",\"id\":40}]}}";
        MyHomeEntity entity = gson.fromJson(tabStr, MyHomeEntity.class);
        setView(entity);

    }

    private void setView(MyHomeEntity entity) {
        if (entity != null && entity.getData() != null) {
            MyHomeEntity.DataBean dataBean = entity.getData();
            List<MyHomeEntity.DataBean.LmListBean> lmList = dataBean.getLmList();
            if (lmList != null && lmList.size() > 0) {
                ArrayList<TabVpEntity> mTabVps = new ArrayList<>();
                for (MyHomeEntity.DataBean.LmListBean item : lmList) {
                    mTabVps.add(new TabVpEntity(item.getName(), MyHomeArticleFragment.newInstance(item.getId())));
                }
                FragPagerAdapter adapter = new FragPagerAdapter(getActivity().getSupportFragmentManager(), mTabVps);
                homeVp.setAdapter(adapter);
                homeTab.setupWithViewPager(homeVp);
                homeVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                        ToastUtils.getInstance(getActivity()).showMessage("当前为第"+position+"个Tab");
                    }

                    @Override
                    public void onPageSelected(int position) {

                            refresh.setEnableLoadMore(false);
                            refresh.setEnableRefresh(true);

                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            }
        }
    }

}